package com.yeeverse.sdk.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
    private static SimpleDateFormat SDFTIME = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat SDFDATE = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 获取当前时间
     *
     * @return
     */
    public static String getCurrentTime() {
        return formatDate(new Date());
    }

    /**
     * 格式化日期
     *
     * @param date
     * @return yyyy-MM-dd HH:mm
     */
    public static String formatDate(Date date) {
        return SDFTIME.format(date);
    }

    /**
     * 格式化日期
     *
     * @param date
     * @return yyyy-MM-dd
     */
    public static Date parseDate(String date) {
        try {
            return SDFDATE.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取当前月份
     *
     * @return
     */
    public static int getCurrentMonth() {
        Calendar cal = Calendar.getInstance();
        return cal.get(Calendar.MONTH) + 1;
    }

    /**
     * 根据时间及时间格式转换成date类型
     *
     * @param time
     * @param timeType
     * @return
     */
    public static Date getDateByString(String time, String timeType) {
        SimpleDateFormat sdfdate = new SimpleDateFormat(timeType);
        try {
            return sdfdate.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 根据时间及时间格式转换成string类型
     *
     * @param time
     * @param timeType
     * @return
     */
    public static String getStringByDate(Date time, String timeType) {
        SimpleDateFormat sdfdate = new SimpleDateFormat(timeType);
        return sdfdate.format(time);
    }

    /**
     * 根据月份获取月份最开始时间
     *
     * @param datetime
     * @return
     */
    public static Date getMonthStart(int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DATE, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 根据月份获取月份结束时间
     *
     * @param datetime
     * @return
     */
    public static Date getMonthEnd(int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 998);
        return cal.getTime();
    }

    /**
     * 根据时间获取当天最开始时间
     *
     * @param datetime
     * @return
     */
    public static Date getDateStart(Date datetime) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(datetime);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 根据时间获取当天结束时间
     *
     * @param datetime
     * @return
     */
    public static Date getDateEnd(Date datetime) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(datetime);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 998);
        return cal.getTime();
    }

    /**
     * 根据时间获取月份最开始时间
     *
     * @param datetime
     * @return
     */
    public static Date getMonthStart(Date datetime) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(datetime);
        cal.set(Calendar.DATE, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 根据时间获取月份结束时间
     *
     * @param datetime
     * @return
     */
    public static Date getMonthEnd(Date datetime) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(datetime);
        cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 998);
        return cal.getTime();
    }


}
